package com.example.hrsystem.workflow.repository;

import android.content.Context;

import com.example.hrsystem.data.source.AppDatabase;
import com.example.hrsystem.workflow.model.NodeExecution;
import com.example.hrsystem.workflow.model.ProcessDefinition;
import com.example.hrsystem.workflow.model.ProcessInstance;
import com.example.hrsystem.workflow.model.WorkflowTask;

import java.util.List;

/**
 * 工作流数据访问仓库实现类
 * 提供工作流相关数据的访问实现
 */
public class WorkflowRepositoryImpl implements WorkflowRepository {
    private AppDatabase database;

    public WorkflowRepositoryImpl(Context context) {
        database = AppDatabase.getDatabase(context);
    }

    @Override
    public ProcessDefinition getProcessDefinitionById(int id) {
        return database.processDefinitionDao().getProcessDefinitionById(id);
    }

    @Override
    public List<ProcessDefinition> getAllProcessDefinitions() {
        return database.processDefinitionDao().getAllProcessDefinitions();
    }

    @Override
    public void saveProcessDefinition(ProcessDefinition processDefinition) {
        database.processDefinitionDao().insertProcessDefinition(processDefinition);
    }

    @Override
    public void updateProcessDefinition(ProcessDefinition processDefinition) {
        database.processDefinitionDao().updateProcessDefinition(processDefinition);
    }

    @Override
    public void deleteProcessDefinition(int id) {
        ProcessDefinition processDefinition = database.processDefinitionDao().getProcessDefinitionById(id);
        if (processDefinition != null) {
            database.processDefinitionDao().deleteProcessDefinition(processDefinition);
        }
    }

    @Override
    public ProcessInstance getProcessInstanceById(int id) {
        return database.processInstanceDao().getProcessInstanceById(id);
    }

    @Override
    public List<ProcessInstance> getProcessInstancesByDefinitionId(int processDefinitionId) {
        return database.processInstanceDao().getProcessInstancesByDefinitionId(processDefinitionId);
    }

    @Override
    public List<ProcessInstance> getProcessInstancesByInitiator(int initiatorId) {
        return database.processInstanceDao().getProcessInstancesByInitiatorId(initiatorId);
    }

    @Override
    public void saveProcessInstance(ProcessInstance processInstance) {
        database.processInstanceDao().insertProcessInstance(processInstance);
    }

    @Override
    public void updateProcessInstance(ProcessInstance processInstance) {
        database.processInstanceDao().updateProcessInstance(processInstance);
    }

    @Override
    public void deleteProcessInstance(int id) {
        ProcessInstance processInstance = database.processInstanceDao().getProcessInstanceById(id);
        if (processInstance != null) {
            database.processInstanceDao().deleteProcessInstance(processInstance);
        }
    }

    @Override
    public WorkflowTask getTaskById(int id) {
        return database.workflowTaskDao().getTaskById(id);
    }

    @Override
    public List<WorkflowTask> getTasksByProcessInstance(int processInstanceId) {
        return database.workflowTaskDao().getTasksByProcessInstanceId(processInstanceId);
    }

    @Override
    public List<WorkflowTask> getTasksByAssignee(int assigneeId) {
        return database.workflowTaskDao().getTasksByAssigneeId(assigneeId);
    }

    @Override
    public List<WorkflowTask> getTasksByStatus(String status) {
        return database.workflowTaskDao().getTasksByStatus(status);
    }

    @Override
    public void saveTask(WorkflowTask task) {
        database.workflowTaskDao().insertTask(task);
    }

    @Override
    public void updateTask(WorkflowTask task) {
        database.workflowTaskDao().updateTask(task);
    }

    @Override
    public void deleteTask(int id) {
        WorkflowTask task = database.workflowTaskDao().getTaskById(id);
        if (task != null) {
            database.workflowTaskDao().deleteTask(task);
        }
    }

    @Override
    public NodeExecution getNodeExecutionById(int id) {
        // 简化实现，实际项目中应该有NodeExecutionDao
        return null;
    }

    @Override
    public List<NodeExecution> getNodeExecutionsByProcessInstance(int processInstanceId) {
        // 简化实现，实际项目中应该有NodeExecutionDao
        return null;
    }

    @Override
    public void saveNodeExecution(NodeExecution nodeExecution) {
        // 简化实现，实际项目中应该有NodeExecutionDao
    }

    @Override
    public void updateNodeExecution(NodeExecution nodeExecution) {
        // 简化实现，实际项目中应该有NodeExecutionDao
    }

    @Override
    public void deleteNodeExecution(int id) {
        // 简化实现，实际项目中应该有NodeExecutionDao
    }
}